package org.groupg.practice.io;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.AgeFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ComplexFileFilterExample {
    public static void main(String[] args) {
        File directory = new File("path/to/directory");

        // 创建复杂的文件过滤器：选择目录下所有以 ".txt" 结尾的文件，且最后修改时间在指定日期之后
        Date cutoffDate = new Date(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(10)); // 10天前
        IOFileFilter fileFilter = new SuffixFileFilter(".txt", IOCase.SENSITIVE)
                .and(new AgeFileFilter(cutoffDate));

        // 列出符合条件的文件
        Collection<File> files = FileUtils.listFiles(directory, fileFilter, TrueFileFilter.INSTANCE);
        for (File file : files) {
            System.out.println("Found file: " + file.getName() + " with last modified: " + file.lastModified());
        }
    }
}
